package com.zysc.processflow.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zysc.processflow.common.base.ResultBody;
import com.zysc.processflow.entity.Handlerecord;
import com.zysc.processflow.entity.Handlerecordcurrentnode;
import com.zysc.processflow.entity.Handlerecorddetail;
import com.zysc.processflow.entity.Handletask;
import com.zysc.processflow.pojo.HandleRecordPO;
import com.zysc.processflow.pojo.RecordOpenParam;
import com.zysc.processflow.service.IHandlerecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import com.zysc.processflow.common.base.BaseController;

import java.util.List;

/**
 * <p>
 * 处置流程记录表 前端控制器
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-18
 */
@Api(value = "处置流程记录", tags = "处置流程记录")
@RestController
@RequestMapping("/handlerecord")
public class HandlerecordController extends BaseController<Handlerecord> {

    @Autowired
    IHandlerecordService service;

    public HandlerecordController(IHandlerecordService service) {
        super(service);
    }

    @ApiOperation("获取处置流程记录列表(支持分页)")
    @GetMapping(value = "GetHandlerecordList")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "pageIndex", name = "pageIndex", dataType = "String"),
            @ApiImplicitParam(value = "pageSize", name = "pageSize", dataType = "String"),
            @ApiImplicitParam(value = "状态", name = "handlestatus", dataType = "String"),
            @ApiImplicitParam(value = "流程实例ID", name = "id", dataType = "String"),
            @ApiImplicitParam(value = "开始时间", name = "begintime", dataType = "Long"),
            @ApiImplicitParam(value = "结束时间", name = "endtime", dataType = "Long")
    })
    public ResultBody GetHandlerecordList(Integer pageIndex , Integer pageSize , String handlestatus,Long begintime , Long endtime ,String id) {
        if (pageIndex != null && pageSize != null) {
            IPage<Handlerecord> page = new Page<>(pageIndex, pageSize);
            List<Handlerecord> res = service.GetHandlerecordListByPage(page, handlestatus,begintime,endtime,id);
            return ResultBody.Success(res, page.getTotal());
        } else {
            List<Handlerecord> res = service.GetHandlerecordListByPage(null, handlestatus,begintime,endtime,id);
            return ResultBody.Success(res);
        }
    }

    @ApiOperation("添加/更新处置流程记录实体")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处置流程记录实体", name = "entity", dataType = "Alarms")
    })
    @Transactional
    @PostMapping(value = "SaveOrUpdate")
    public ResultBody SaveOrUpdate(@RequestBody Handlerecord entity) {
        Boolean b = service.saveOrUpdate(entity);
        if(b){
            return ResultBody.Success(entity);
        }else {
            return ResultBody.error("添加/更新数据失败", 500);
        }
    }

    @ApiOperation("删除处置流程记录")
    @PostMapping("DeleteHandlerecord")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "处置流程记录实体id", name = "id", dataType = "String")
    })
    public ResultBody DeleteHandlerecord(String id) {
        try {
            //1.删除记录
            Boolean b = service.deleteRecordById(id);
            if(!b){
                throw new Exception("删除处置流程记录失败");
            }
            return ResultBody.Success(true);
        }catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error("删除处置流程记录失败: "+e.getMessage(), 500);
        }
    }

    @ApiOperation("开启一条记录")
    @PostMapping(value = "startProcess")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(value = "开启流程必备参数", name = "entity", dataType = "RecordOpenParam")
    })
    public ResultBody startProcess(@RequestBody RecordOpenParam entity) {
        try {
            //1.校验数据有效性
            Boolean b = service.checkRecordValid(entity);
            //2.开启记录
            HandleRecordPO recordFullInfo = service.startRecord(entity);
            return ResultBody.Success(recordFullInfo);
        }catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultBody.error("开启记录失败: "+e.getMessage(), 500);
        }
    }

    @ApiOperation("获取一条记录的全部信息,根据recordid")
    @GetMapping(value = "getInfoByRecordid")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "处置流程记录实体id", name = "recordid", dataType = "String")
    })
    public ResultBody getRecordFullInfoByRecordId(String recordid) {
        try {
            HandleRecordPO handleRecordPO = service.getRecordFullInfoByRecordId(recordid);
            return ResultBody.Success(handleRecordPO);
        }catch (Exception e) {
            return ResultBody.error("获取记录失败: "+e.getMessage(), 500);
        }
    }
}
